package com.kunya.product.service.impl.specification;

//import com.codingapi.tx.annotation.TxTransaction;
import com.kunya.common.common.ServerResponse;
import com.google.common.collect.Lists;
import com.kunya.common.common.ResponseCode;
import com.kunya.common.exception.ServerException;
import com.kunya.common.util.ArrayUtils;
import com.kunya.common.util.RandomUtils;
import com.kunya.product.dao.specification.KindAttributeMapper;
import com.kunya.product.dao.specification.ProductSpecificationMapper;
import com.kunya.product.po.ProductSpecification;
import com.kunya.product.service.specofocation.IProductSpecificationService;
import com.kunya.product.vo.ProductSpecificationVo;
import com.kunya.product.vo.SpecificationKindVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author zp
 * @Description: 商品规格属性service实现类。对应os_product_specification表的相关操作，此表描述了商品最小库存单位
 * @date 2018/6/19 11:06
 */
@Service("iProductSpecificationService")
public class ProductSpecificationImpl implements IProductSpecificationService {

    @Autowired
    private ProductSpecificationMapper productSpecificationMapper;

    @Autowired
    private KindAttributeMapper kindAttributeMapper;

    @Autowired
    private RestTemplate restTemplate;

    /**
     * @author zp
     * @Description:根据商品id获取商品规格属性表实体volist
     * @param productId 商品id tableprefix 表前缀
     * @date 2018/7/3 14:55
     */
    @Override
    public ServerResponse< List<ProductSpecificationVo>> getProductSpecById(Long productId,Byte status,String tableprefix){
        if(productId==null|| StringUtils.isEmpty(tableprefix)||status==null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(),ResponseCode.ILLEGAL_ARGUMENT.getDesc());
        }
        List<ProductSpecification> productSpecifications=productSpecificationMapper.selectByProductId(productId,status,tableprefix);
        if(productSpecifications.isEmpty()){
            return ServerResponse.createByErrorMessage("获取商品规格属性失败");
        }
        List<ProductSpecificationVo> productSpecificationVos= Lists.newArrayList();
        ProductSpecificationVo productSpecificationVo=new ProductSpecificationVo();
        for (ProductSpecification productSpecification: productSpecifications ){
            productSpecificationVo=assembleProductSpecificationVo(productSpecification);
            productSpecificationVos.add(productSpecificationVo);
        }
        return  ServerResponse.createBySuccess(productSpecificationVos);
    }

    @Override
    public ServerResponse deleteProductSpecificationByProductId(Long productId, String tablePrefix) {
        return productSpecificationMapper.deleteProductSpecificationByProductId(productId, tablePrefix) >0 ?
                ServerResponse.createBySuccess("删除数据成功"):ServerResponse.createByErrorMessage("删除数据异常");
    }

    /**
     * @author zp
     * @Description: 组装ProductSpecificationVo
     * @param
     * @date 2018/7/4 14:18
     */
    private ProductSpecificationVo assembleProductSpecificationVo(ProductSpecification productSpecification){
        ProductSpecificationVo productSpecificationVo=new ProductSpecificationVo();
        BeanUtils.copyProperties(productSpecification,productSpecificationVo);
        return productSpecificationVo;
    }

    /**
     * 添加商品规格
     * @param productId
     * @param kindAttrIds
     * @param specificationIds
     * @param tablePrefix
     * @return
     */
    @Override
    @Transactional
//    @TxTransaction(isStart = true)
    public ServerResponse addProductSpecification(Long productId, List<Long> kindAttrIds, List<Long> specificationIds, String tablePrefix) {
        List<Long> addedSpecAttrIds=new ArrayList<>();
        //1.根据商品ID查询出已有的商品规格和分类
        for (SpecificationKindVo specificationKindVo : productSpecificationMapper.getSpecificationAndKindByProductId(productId,kindAttrIds,tablePrefix)) {
            specificationKindVo.setCreateTime(new Date());
            //1.1 插入kindAttribute表
            kindAttributeMapper.insertkindAttrByMiddle(specificationKindVo, tablePrefix);
            if (specificationKindVo.getSpecAttrId() != null) {
                addedSpecAttrIds.add(specificationKindVo.getSpecAttrId());
            }
        }
        ProductSpecification productSpecification=new ProductSpecification();
        productSpecification.setProductId(productId);
        productSpecification.setProductSpecNumber(productId+RandomUtils.randomLongValue(5));
        productSpecification.setDefaultStatus((byte)1);

        ServerResponse serverResponse=restTemplate.getForObject("http://order/order/updateOrderStatus?orderId=157&status=8&tablePrefix="+tablePrefix,ServerResponse.class);
        int a=100/0;
        //TODO 从Shrio当中获取当前的用户名
        productSpecification.setCreateBy("Suimg");
        productSpecification.setUpdateBy("Suimg");
        productSpecification.setCreateTime(new Date());
        productSpecification.setUpdateTime(new Date(0));
        productSpecification.setSpec(ArrayUtils.toString(addedSpecAttrIds));
        //productSpecification.setKindAttrIds(ArrayUtils.toString(addedSpecAttrIds));
        productSpecification.setSalesVolume(0);
        if(productSpecificationMapper.insert(productSpecification, tablePrefix)!=1){
            throw new ServerException("添加商品规格失败!");
        }
        return ServerResponse.createBySuccess("添加商品规格成功.");
    }

}
